package com.lanou.dllo.day14.other;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lanou.dllo.day14.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by dllo on 16/1/26.
 */
public class OtherAdapter extends RecyclerView.Adapter<OtherAdapter.MyViewHolder> {

    Context context;
    List<String> data;
    Random random;//用来产生随机数
    List<Integer> heights;//存放每一条的高度

    public OtherAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
        random = new Random();
        heights = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            data.add("这是第" + i + "条");
            heights.add(random.nextInt(200) + 100);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_other, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        //可以通过params来设置textView的宽高
        ViewGroup.LayoutParams params = holder.textView.getLayoutParams();
        //可以直接设置高度,单位是像素
        params.height = heights.get(position);

        if (data != null && data.size() > 0) {
            holder.textView.setText(data.get(position));
        }

    }
    //向数据集合的第一个位置插入数据
    public void  insetToZero() {
        data.add(0, "这是新的数据");
        heights.add(0, random.nextInt(200) + 100);
        notifyItemChanged(1);

    }
    public void desZero(){
        data.remove(0);
        heights.remove(0);
        //通知适配器删除0位置数据
        notifyItemRemoved(0);

    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public MyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.other_tv);
        }
    }



}
